package leetcode;

import java.util.Stack;

/**
 * @Classname IsValid
 * @Author happytaohaha
 * @Date 2020/3/12
 * @Description 一个栈搞定
 */
public class IsValid_0020 {


    public boolean isValid(String s) {
        if(s.length()<1){
            return true;
        }
        boolean flag = true;
        Stack<Character> stack = new Stack<Character>();
        for(int i = 0; i < s.length(); i++){
            if(s.charAt(i)=='('||s.charAt(i)=='['||s.charAt(i)=='{'){
                stack.add(s.charAt(i));
            }else{
                if(stack.size()<1){
                    flag = false;
                    break;
                }
                char pop = stack.pop();
                if(pop=='('&&s.charAt(i)==')'){
                    continue;
                }else if(pop=='{'&&s.charAt(i)=='}'){
                    continue;
                }else if(pop=='['&&s.charAt(i)==']'){
                    continue;
                }else{
                    flag =false;
                    break;
                }
            }
        }
        if(flag&&stack.size()==0){
            return true;
        }
        return false;

    }
}
